﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    public partial class MysterylForm : Form
    {
        private OpenFileDialog fDialog = new OpenFileDialog();
        private string text;
        private int swordHits = 0;
        private int procRateWeapon = 0;
        private int miss = 0;

        public MysterylForm()
        {
            InitializeComponent();
            userDefinedTextbox.Text = "seems to be reduced";
        }

        private void readButton_Click(object sender, EventArgs e)
        {
            this.swordHits = 0;
            this.procRateWeapon = 0;
            this.miss = 0;

            fDialog.Title = "Open File";                                // Titel des Dialogfensters
            if (fDialog.ShowDialog() == DialogResult.OK)                // Es gab eine Auswahl
            {
                filePathTextBox.Text = fDialog.FileName.ToString();     // filePathTextBox bekommt den Pfad zugewiesen
                iniTextBox.Text = File.ReadAllText(filePathTextBox.Text);
            }           
        }

        private void parseButton_Click(object sender, EventArgs e)
        {
             this.swordHits = 0;
             this.procRateWeapon = 0;
             this.miss = 0;
            
             text = File.ReadAllText(filePathTextBox.Text);

             Regex swordRegex = new Regex(@"with your sword and hit");  
             Regex armorReducedRegex = new Regex(@""+userDefinedTextbox.Text);
             Regex missesRegex = new Regex(@"You miss");
                         
             Match m = swordRegex.Match(text);
             while (m.Success)                 
             {
                 this.swordHits++;
                 m = m.NextMatch();
             }

             Match m2 = armorReducedRegex.Match(text);
             while (m2.Success)
             {
                 this.procRateWeapon++;
                 m2 = m2.NextMatch();
             }

             Match m3 = missesRegex.Match(text);
             while (m3.Success)
             {
                 this.miss++;
                 m3 = m3.NextMatch();
             }

             this.swordTextBox.Text = Convert.ToString(this.swordHits);
             this.procRateWeaponTextBox.Text = Convert.ToString(this.procRateWeapon);
             this.missesTextBox.Text = Convert.ToString(this.miss);
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
